/**
 * 				MochitoClientGui.java
 *				------------
 *		
 *   Author:	Moser Dominik
 *   ID:		bin04017
 *	 Created:	18.12.2006
 *	 Project:	Mochito-MP3-Sync
 *	 Package:	gui
 *	 Usage:		
 */
package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import mochito.clientClasses.ClientManager;

/**
 * @author bin04017
 * 
 */
public class MochitoClientGui extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JSplitPane jSplitPane = null;

	private JPanel jPanel = null;

	private JScrollPane jScrollPane = null;

	private JList jList = null;

	private JButton jButton = null;

	private JButton jButton1 = null;

	private ClientManager client = null;

	private DefaultListModel defaultListModel;

	private JTextField jTextField = null;
	
	/**
	 * This is the default constructor
	 */
	public MochitoClientGui(String host) {
		super();
		try {
			client = new ClientManager(host);
		} catch (Exception e1) {
			System.err.println(e1.toString());
		}
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(459, 316);
		this.setContentPane(getJContentPane());
		this.setTitle("MochitoClient");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.exit(0);
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getJSplitPane() {
		if (jSplitPane == null) {
			jSplitPane = new JSplitPane();
			jSplitPane.setLeftComponent(getJPanel());
			jSplitPane.setRightComponent(getJScrollPane());
		}
		return jSplitPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(getJButton(), BorderLayout.NORTH);
			jPanel.add(getJButton1(), BorderLayout.EAST);
			jPanel.add(getJTextField(), BorderLayout.SOUTH);
		}
		return jPanel;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJList());
		}
		return jScrollPane;
	}

	private DefaultListModel getDefaultListModel() {
		if (defaultListModel == null) {
			defaultListModel = new DefaultListModel();
		}
		return defaultListModel;
	}

	/**
	 * This method initializes jList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getJList() {
		if (jList == null) {
			jList = new JList(getDefaultListModel());
		}
		return jList;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Refresh");
			jButton.addActionListener(this);
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Activate");
			jButton1.addActionListener(this);
		}
		return jButton1;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(jButton)) {
			this.refresh();
		} else if (e.getSource().equals(jButton1)) {
			try {
				this.activate();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

	private void activate() throws Exception {
		for (int index = 0; index < getDefaultListModel().getSize(); index++) {
			client.setTimerForMusicItem(client.getMusicItem(
					(String) getDefaultListModel().get(index), this.jTextField
							.getText()));
		}
		client.startTimer();
	}

	private void refresh() {
		try {
			this.getDefaultListModel().clear();
			for (String item : client.getMusicItems()) {
				this.getDefaultListModel().addElement(item);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setText("c:/temp/");
			jTextField.setEditable(false);
		}
		return jTextField;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
